package com.wwinsoft.framework.web.system.Taglib;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.wwinsoft.framework.entity.system.Code;
import com.wwinsoft.framework.service.system.CodeManager;

@SuppressWarnings("unchecked")
public class CodeListTag extends TagSupport {

    private String codeType;
    private String id;
    private String name;
    private String codeValue;
    private String onChange;
    private String cssClass;
    private boolean displayHeader;

    private String table;
    private String codeColumn;
    private String valueColumn;
    private String where;

    public int doStartTag() throws JspException {
        ServletContext sc = pageContext.getServletContext();

        WebApplicationContext webAppCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        CodeManager codeManager = (CodeManager) webAppCtx.getBean("codeManager");
        List<Code> codeList;
        if(StringUtils.isNotEmpty(codeType)){
            codeList = codeManager.getCodeListByType(codeType);
        }else{
            codeList = codeManager.getCodeListByTable(table, codeColumn, valueColumn, where);
        }

        JspWriter out = pageContext.getOut();
        StringBuffer html = new StringBuffer("");
        html.append("<select name='").append(name).append("'");
        if (StringUtils.isNotEmpty(id)) {
            html.append(" id='").append(id).append("'");
        }
        if (StringUtils.isNotEmpty(onChange)) {
            html.append(" onChange='").append(onChange).append("'");
        }
        if (StringUtils.isNotEmpty(cssClass)) {
            html.append(" class='").append(cssClass).append("'");
        }
        html.append(">");
        if (displayHeader) {
            html.append("<option value=''>选择 </option>");
        }
        try {
            for (Code o : codeList) {
                String code = o.getCode();
                String value = o.getValue();
                String selected = value.equals(codeValue) ? "selected" : "";
                html.append("<option value='").append(value).append("' ")
                        .append(selected).append(">").append(code).append("</option>");
            }
            html.append("</select>");
            out.println(html);
        } catch (Exception e) {
            throw new JspException(e);
        }
        return super.doStartTag();
    }

    public void setCodeType(String codeType) {
        this.codeType = codeType;
    }

    public void setCodeValue(String codeValue) throws JspException {
        this.codeValue = (String) ExpressionEvaluatorManager.evaluate("codeValue", codeValue, String.class, this,
                                                                      pageContext);
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setOnChange(String changeAction) {
        this.onChange = changeAction;
    }

    public String getCssClass() {
        return cssClass;
    }

    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }

    public Boolean isDisplayHeader() {
        return displayHeader;
    }

    public void setDisplayHeader(Boolean displayHeader) {
        this.displayHeader = displayHeader;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public String getCodeColumn() {
        return codeColumn;
    }

    public void setCodeColumn(String codeColumn) {
        this.codeColumn = codeColumn;
    }

    public String getValueColumn() {
        return valueColumn;
    }

    public void setValueColumn(String valueColumn) {
        this.valueColumn = valueColumn;
    }

    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }
}